﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEngine;

namespace ProjScan.Tools
{
	// Token: 0x02000162 RID: 354
	internal class FailedResultManager
	{
		// Token: 0x06000CAB RID: 3243 RVA: 0x00006747 File Offset: 0x00004947
		public void Init()
		{
			this.path = ToolsHelper.GetFailResultPath();
			WhiteAssetManager.Instance.Init();
		}

		// Token: 0x06000CAC RID: 3244 RVA: 0x0003833C File Offset: 0x0003653C
		public void UpdateFailedData(string key, Dictionary<string, FailedAssetInfo> failedDataDic)
		{
			bool flag = this.dicFails.ContainsKey(key);
			if (flag)
			{
				try
				{
					this.dicFails[key] = failedDataDic;
				}
				catch (Exception exception)
				{
					FixLog.Instance.LogException(FixLog.FixStage.f5, exception, "", "");
				}
			}
		}

		// Token: 0x06000CAD RID: 3245 RVA: 0x0003839C File Offset: 0x0003659C
		public void LoadAll()
		{
			string text = Path.Combine(Application.dataPath, this.path);
			bool flag = Directory.Exists(text);
			if (flag)
			{
				int num = 0;
				ToolsHelper.ShowProgressBar("Failed Data", "Loading Failed Data...", 0f, false);
				foreach (string text2 in Directory.GetFiles(text, "*.csv"))
				{
					num++;
					string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(text2);
					this.LoadFailedData(fileNameWithoutExtension);
					bool flag2 = num % 5 == 0;
					if (flag2)
					{
						ToolsHelper.ShowProgressBar("Failed Data", "Loading Failed Data...", 1f / (float)num, false);
					}
				}
				ToolsHelper.ClearProgressBar();
			}
		}

		// Token: 0x06000CAE RID: 3246 RVA: 0x0003844C File Offset: 0x0003664C
		public void LoadFailedData(string ruleKey)
		{
			string filePath = Path.Combine(Application.dataPath, this.path) + ruleKey + ".csv";
			bool flag = File.Exists(filePath);
			if (flag)
			{
				this.LoadFailedData(filePath, ruleKey);
			}
		}

		// Token: 0x06000CAF RID: 3247 RVA: 0x0003848C File Offset: 0x0003668C
		private bool LoadFailedData(string filePath, string keyPrefix)
		{
			bool result;
			try
			{
				bool flag = this.dicFails.ContainsKey(keyPrefix);
				if (flag)
				{
					this.dicFails[keyPrefix].Clear();
				}
				using (StreamReader streamReader = new StreamReader(filePath))
				{
					int num = -1;
					int num2 = -1;
					int num3 = -1;
					int num4 = -1;
					string text = ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)";
					Regex regex = new Regex(text);
					bool flag2 = !streamReader.EndOfStream;
					if (flag2)
					{
						string text2 = streamReader.ReadLine();
						string[] array = regex.Split(text2);
						for (int i = 0; i < array.Length; i++)
						{
							string text3 = ToolsHelper.TrimYH(array[i]);
							string a = text3;
							if (!(a == "Location"))
							{
								if (!(a == "ShouldBeFixed"))
								{
									if (!(a == "ShouldJoinWhiteList"))
									{
										if (a == "IsFixed")
										{
											num4 = i;
										}
									}
									else
									{
										num3 = i;
									}
								}
								else
								{
									num2 = i;
								}
							}
							else
							{
								num = i;
							}
						}
					}
					int num5 = 0;
					while (!streamReader.EndOfStream)
					{
						string text4 = streamReader.ReadLine();
						num5++;
						string[] array2 = regex.Split(text4);
						try
						{
							string key = ToolsHelper.TrimYH(array2[num]);
							bool shouldBeFixed = bool.Parse(ToolsHelper.TrimYH(array2[num2]));
							bool shouldJoinWhiteList = bool.Parse(ToolsHelper.TrimYH(array2[num3]));
							bool isFixed = bool.Parse(ToolsHelper.TrimYH(array2[num4]));
							FailedAssetInfo value = new FailedAssetInfo
							{
								ShouldBeFixed = shouldBeFixed,
								ShouldJoinWhiteList = shouldJoinWhiteList,
								IsFixed = isFixed
							};
							bool flag3 = !this.dicFails.ContainsKey(keyPrefix);
							if (flag3)
							{
								this.dicFails[keyPrefix] = new Dictionary<string, FailedAssetInfo>();
							}
							this.dicFails[keyPrefix][key] = value;
						}
						catch (Exception exception)
						{
							FixLog.Instance.LogException(FixLog.FixStage.f3, exception, "LoadFailedData", string.Format("Error while adding failed data from {0} with line {1}", keyPrefix, num5));
						}
					}
				}
				result = true;
			}
			catch (Exception exception2)
			{
				FixLog.Instance.LogException(FixLog.FixStage.f3, exception2, "LoadFailedData", "Error while loading failed data from " + filePath);
				result = false;
			}
			return result;
		}

		// Token: 0x06000CB0 RID: 3248 RVA: 0x0003870C File Offset: 0x0003690C
		public void SaveFailResultInfo(string ruleKey)
		{
			bool flag = this.dicFails.ContainsKey(ruleKey);
			if (flag)
			{
				string value = "\"Location\",\"ShouldBeFixed\",\"ShouldJoinWhiteList\",\"IsFixed\"";
				string str = Path.Combine(Application.dataPath, this.path);
				string text = str + ruleKey + ".csv";
				bool flag2 = !Directory.Exists(str);
				if (flag2)
				{
					Directory.CreateDirectory(str);
				}
				bool flag3 = !File.Exists(text);
				if (flag3)
				{
					File.Create(text).Dispose();
				}
				using (StreamWriter streamWriter = new StreamWriter(text))
				{
					streamWriter.WriteLine(value);
					foreach (KeyValuePair<string, FailedAssetInfo> keyValuePair in this.dicFails[ruleKey])
					{
						string[] values = new string[]
						{
							ToolsHelper.QuoteString(keyValuePair.Key),
							",",
							keyValuePair.Value.ShouldBeFixed.ToString(),
							",",
							keyValuePair.Value.ShouldJoinWhiteList.ToString(),
							",",
							keyValuePair.Value.IsFixed.ToString()
						};
						string value2 = string.Concat(values);
						streamWriter.WriteLine(value2);
					}
				}
			}
		}

		// Token: 0x06000CB1 RID: 3249 RVA: 0x00038888 File Offset: 0x00036A88
		public void SaveAll()
		{
			foreach (string ruleKey in this.dicFails.Keys)
			{
				this.SaveFailResultInfo(ruleKey);
			}
		}

		// Token: 0x040007BA RID: 1978
		public static readonly FailedResultManager Instance = new FailedResultManager();

		// Token: 0x040007BB RID: 1979
		public Dictionary<string, Dictionary<string, FailedAssetInfo>> dicFails = new Dictionary<string, Dictionary<string, FailedAssetInfo>>();

		// Token: 0x040007BC RID: 1980
		private string path = "";
	}
}
